import {Component, OnInit, ViewEncapsulation, ViewChild} from '@angular/core';
import {SeedInfiniteScroll} from '@shared/components/infinite-scroll/seed-infinite-scroll/seed-infinite-scroll.component';
import {ActivatedRoute, Router} from '@angular/router';
import {TrainService} from "@train/train.service";
import {Content} from "@shared/components/toolbar/toolbar-content";
import {AuthService} from "@shared/service/auth/auth.service";

@Component({
  selector: 'seed-course-list',
  templateUrl: './course.component.html',
  styleUrls: ['./course.component.scss'],
  encapsulation: ViewEncapsulation.None
})
export class CourseComponent implements OnInit {
  public course: any;
  public properties: string;
  public params: {
    classId: string,
    beginTime: string,
    endTime: string,
    teacherId: string,
    page: 1,
    rows: 10,
    // orgUserId: string,
  };
  @ViewChild(Content) content;
  @ViewChild(SeedInfiniteScroll) infiniteScroll;

  constructor(
    private trainService: TrainService,
    private route: ActivatedRoute,
    private router: Router,
    private authSer: AuthService
  ) {
    /*this.course = {};
    this.course['list'] = [];
    this.course['total'] = <number>0;*/
    this.course = {
      list : [],
      total: 0
    };
    this.route.queryParams.subscribe((param) => {
      this.properties = param['properties'];
      this.params = {
        classId: param['classId'],
        beginTime: param['beginTime'],
        endTime: param['endTime'],
        teacherId: param['teacherId'],
        page: 1,
        rows: 10,
        // orgUserId: this.authSer.getAuthOrgId()
      };
    });
  }

  /**
   * @description 查询课时列表
   * @author 李浩-1157
   * @date 2017/09/06
   * @param classId 班级id
   */
  getCourseList() {
    this.trainService.getCourseList(this.params).subscribe(res => {
      if (res.result === '0' && res.data) {
        const course = this.course;
        this.params['page']++;
        if (this.infiniteScroll) {
          this.infiniteScroll.complete();
        }
        course.list = course.list.concat(res.data.list);
        course.total = res.data.total;

        if (res.data.total > 0) {
          course['hasData'] = true;
        }
        if (course.list.length === 0) {
          course['visible'] = true;
        } else {
          course['visible'] = false;
        }
        if (!res.data.hasNextPage && this.infiniteScroll) {
          this.infiniteScroll.enable(false);
        }
      }
    });
  }


  /**
   * @description 跳转签到
   * @author 李浩-1157
   * @date 2017/09/14
   */
  goToRollCall(classScheduleId, courseName) {
    this.router.navigate(['/train/roll-call'], {
      queryParams: {
        classScheduleId: classScheduleId,
        teacherId: this.params.teacherId,
        courseName: courseName,
        properties: this.properties
      }
    });
  }

  ngOnInit() {
    this.getCourseList();
  }
}
